package com.dream.keigezhushou.Activity.activity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.adapter.NineGridAdapter;
import com.dream.keigezhushou.Activity.bean.CommontInfo;
import com.dream.keigezhushou.Activity.bean.Image;
import com.dream.keigezhushou.Activity.view.NineGridLayout;
import com.dream.keigezhushou.R;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AppraiseActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.tvcommom)
    TextView mTvcommom;
    @BindView(R.id.list_common)
    PullToRefreshListView mPull;
    @BindView(R.id.activity_appraise)
    LinearLayout mActivityAppraise;

    private ArrayList<CommontInfo> list = new ArrayList<>();

    private ArrayList<CommontInfo> commmonlist = new ArrayList<>();
    private List<List<Image>> imagesList = new ArrayList<>();
    private String[][] images = new String[][]{
            {"file:///android_asset/img2.jpg", "250", "250"}
            , {"file:///android_asset/img3.jpg", "250", "250"}
            , {"file:///android_asset/img4.jpg", "250", "250"}
            , {"file:///android_asset/img5.jpg", "250", "250"}
            , {"file:///android_asset/img6.jpg", "250", "250"}
            , {"file:///android_asset/img7.jpg", "250", "250"}
            , {"file:///android_asset/img8.jpg", "250", "250"}
    };


    private ArrayList<Image> mItemList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appraise);
        ButterKnife.bind(this);


        ListView mlist = mPull.getRefreshableView();


        initData();


        mlist.setFocusable(false);
        mlist.setAdapter(new CommentAdapter());

    }

    private void initData() {

        for (int i = 0; i < 7; i++) {
            mItemList = new ArrayList<>();
            for (int j = 0; j <= i; j++) {
                mItemList.add(new Image(images[j][0], Integer.parseInt(images[j][1]), Integer.parseInt(images[j][2])));

            }
            imagesList.add(mItemList);
        }

        for (int i = 0; i < 4; i++) {
            CommontInfo info = new CommontInfo();
            info.setDes("环境不错，音响也可以，赠送小吃赞个先，经常 来这，性价比高");
            info.setName("好孩子");
            info.setArr(images);
            info.setIv(getResources().getDrawable(R.mipmap.dsdd));
            info.setPingfen("4");

            commmonlist.add(info);
        }

    }

    class CommentAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return commmonlist.size();
        }

        @Override
        public CommontInfo getItem(int position) {
            return commmonlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            CommonViewHolder holder = null;
            if (convertView == null) {
                convertView = UiUtils.inflateView(R.layout.item_commont_teamdetails);
                holder = new CommonViewHolder();

                holder.iv = (ImageView) convertView.findViewById(R.id.icon);

                holder.name = (TextView) convertView.findViewById(R.id.title);

                holder.des = (TextView) convertView.findViewById(R.id.des);

                holder.nineLayout = (NineGridLayout) convertView.findViewById(R.id.nine_grid_layout);
                holder.pf = (RatingBar) convertView.findViewById(R.id.ratingBar);

                convertView.setTag(holder);

            } else {
                holder = (CommonViewHolder) convertView.getTag();
            }


            CommontInfo info = getItem(position);

            holder.iv.setImageDrawable(info.getIv());
            holder.name.setText(info.getName());
            holder.des.setText(info.getDes());
            holder.pf.setRating(Float.parseFloat(info.getPingfen()));
            //   pf.setRating(Float.valueOf(info.getPingfen()));


            List<Image> itemList = imagesList.get(position);

            if (itemList.isEmpty()) {

                holder.nineLayout.setVisibility(View.GONE);
            } else {
                holder.nineLayout.setVisibility(View.VISIBLE);

                handlerOneImage(holder, itemList, getApplicationContext());
            }


            return convertView;
        }

        private void handlerOneImage(CommonViewHolder holder, List<Image> itemList, Context context) {
            holder.nineLayout.setAdapter(new Gameadapter(context, mItemList));
            holder.nineLayout.setOnItemClickListerner(new NineGridLayout.OnItemClickListerner() {
                @Override
                public void onItemClick(View view, int position) {
                    //do some thing

                }
            });
        }


    }
    static class CommonViewHolder {
        ImageView iv;
        TextView name;

        TextView des;
        NineGridLayout nineLayout;
        TextView common;
        RatingBar pf;
    }

    class Gameadapter extends NineGridAdapter {


        public Gameadapter(Context context, List list) {
            super(context, list);
        }

        @Override
        public int getCount() {
            return (list == null) ? 0 : list.size();
        }

        @Override
        public String getUrl(int positopn) {
            return getItem(positopn) == null ? null : ((Image) getItem(positopn)).getUrl();
        }

        @Override
        public Object getItem(int position) {
            return (list == null) ? null : list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int i, View view) {

            ImageView iv = null;
            if (view != null && view instanceof ImageView) {
                iv = (ImageView) view;
            } else {
                iv = new ImageView(context);
            }
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.setBackgroundColor(context.getResources().getColor((android.R.color.transparent)));
            String url = getUrl(i);
            Picasso.with(context).load(getUrl(i)).placeholder(new ColorDrawable(Color.parseColor("#f5f5f5"))).into(iv);
            if (!TextUtils.isEmpty(url)) {
                iv.setTag(url);
            }
            return iv;
        }
    }

}
